using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Clase20120627
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Effect PointSpriteEffect;
        VertexPositionColor[] SpriteArray;
        Matrix viewMatrix, proyectioMatrix;
        Vector3 CamPos = new Vector3(0, 0, 0);
        float rotation=0;

        AudioEngine audioEngine;
        WaveBank waveBank;
        SoundBank soundBank;

        void loadAudio()
        {
            audioEngine = new AudioEngine("Content/sound.xgs");

        }

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            proyectioMatrix = Matrix.CreatePerspective(800, 600, 1, 1000);
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            PointSpriteEffect = Content.Load<Effect>("PointSprite");
            PointSpriteEffect.Parameters["SpriteTexture"].SetValue(Content.Load<Texture2D>("particle"));
            SpriteArray = new VertexPositionColor[200];
            Random rand = new Random();
            for (int i = 0; i < SpriteArray.Length; i++)
            {
                SpriteArray[i].Position = new Vector3(rand.Next(-1000, 1000) / 10f, rand.Next(-1000, 1000) / 10f, rand.Next(-1000, 1000) / 10f);
                SpriteArray[i].Color = Color.Snow;

            }
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            rotation += 0.01f;
  if (rotation > 2*3.14) {
    rotation -= 2.0f *3.14f;
  }
  float x = 10 * (float) Math.Sin((double) rotation);
  float z = 10 * (float) Math.Cos(rotation) ;
  CamPos = new Vector3(x, 1.5f, z);

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            viewMatrix = Matrix.CreateLookAt(CamPos, new Vector3(0, 0, 0), Vector3.Up);
            GraphicsDevice.Clear(Color.CornflowerBlue);
            graphics.GraphicsDevice.RenderState.PointSpriteEnable = true;
            graphics.GraphicsDevice.RenderState.PointSize = 32.0f;
            graphics.GraphicsDevice.RenderState.AlphaBlendEnable = true;
            graphics.GraphicsDevice.RenderState.SourceBlend = Blend.One;
            graphics.GraphicsDevice.RenderState.DestinationBlend = Blend.One;
            graphics.GraphicsDevice.RenderState.DepthBufferWriteEnable = false;
            graphics.GraphicsDevice.VertexDeclaration = new VertexDeclaration(graphics.GraphicsDevice, VertexPositionColor.VertexElements);
            Matrix WVPMatrix = Matrix.Identity * viewMatrix * proyectioMatrix;
            PointSpriteEffect.Parameters["WVPMatrix"].SetValue(WVPMatrix);

            PointSpriteEffect.Begin();
            foreach (EffectPass pass in PointSpriteEffect.CurrentTechnique.Passes)
            {
                pass.Begin();
                graphics.GraphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.PointList, SpriteArray, 0, SpriteArray.Length);
                pass.End();

            }
            PointSpriteEffect.End();

            graphics.GraphicsDevice.RenderState.PointSpriteEnable = false;
            graphics.GraphicsDevice.RenderState.DepthBufferWriteEnable = true;
            graphics.GraphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha;
            graphics.GraphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha;


            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}
